#include "stdafx.h"
#include "pathutils.h"

#include <memory>
#include "W2MB.h"

namespace PathUtils
{
	CString GetExecutePath(void)
	{
		TCHAR path[MAX_PATH] = {0};
		memset(path, 0, sizeof(char) * MAX_PATH);

		DWORD ret = GetModuleFileName(NULL, path, MAX_PATH);

		path[ret] = '\0';

		CString exe_path = path;

		size_t nPos = exe_path.ReverseFind(_T('\\'));

		if (nPos >= 0)
		{
			return exe_path.Mid(0, nPos);
		}

		return _T("");
	}

	void GetAllFileNames(const CString & path, 
		const CString & filter, 
		CStringArray & file_names,
		BOOL bIncludeSubDirectory)
	{
		WIN32_FIND_DATA find_data;

		CString find_file_name = path;

		find_file_name.Append(_T("\\"));
		find_file_name.Append(filter);

		HANDLE hFind = FindFirstFile(find_file_name,
			&find_data);

		if (hFind != INVALID_HANDLE_VALUE)
		{
			do
			{
				CString file_name = path;
				file_name.Append(_T("\\"));
				file_name.Append(find_data.cFileName);

				file_names.Add(file_name);
			}
			while(FindNextFile(hFind, &find_data));

			FindClose(hFind);
		}

		//Handle Directories
		if (bIncludeSubDirectory)
		{
			find_file_name = path;

			find_file_name.Append(_T("\\*"));

			HANDLE hFind = FindFirstFile(find_file_name,
				&find_data);

			if (hFind == INVALID_HANDLE_VALUE)
				return;

			do
			{
				if (((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) &&
					(_tcsncmp(find_data.cFileName, _T("."), _tcslen(_T("."))) != 0) &&
					(_tcsncmp(find_data.cFileName, _T(".."), _tcslen(_T(".."))) != 0))
				{
					CString file_name = path;
					file_name.Append(_T("\\"));
					file_name.Append(find_data.cFileName);

					GetAllFileNames(file_name,
						filter,
						file_names,
						bIncludeSubDirectory);
				}
			}
			while(FindNextFile(hFind, &find_data));

			FindClose(hFind);
		}//if bIncludeSubDirectory
	}

	BOOL DoesDirectoryExists(const CString & path)
	{
		WIN32_FIND_DATA find_data;

		HANDLE hFind = FindFirstFile(path,
			&find_data);

		if (hFind != INVALID_HANDLE_VALUE)
		{
			FindClose(hFind);

			return (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 
				FILE_ATTRIBUTE_DIRECTORY;
		}

		return FALSE;
	}

	BOOL DoesFileExists(const CString & filename)
	{
		WIN32_FIND_DATA find_data;

		HANDLE hFind = FindFirstFile(filename,
			&find_data);

		if (hFind != INVALID_HANDLE_VALUE)
		{
			FindClose(hFind);

			return (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 
				FILE_ATTRIBUTE_DIRECTORY;
		}

		return FALSE;
	}

	BOOL IsPathRooted(const CString & path)
	{
#ifdef _WIN32_CE
		if (path.GetLength() >= 1 && path[0] == '\\')
			return TRUE;
#endif
		if (path.GetLength() >= 2 && path[1] == ':')
			return TRUE;

		return FALSE;
	}

	BOOL GetFileContent(const CString & file_path, CString & content)
	{
		CFile file;

		if (file.Open(file_path, CFile::modeRead))
		{
			int size = (int)file.GetLength();

			std::auto_ptr<char> buf(new char[size + 1]);

			int read = file.Read(buf.get(), size);

			buf.get()[read] = 0;

			if (read != size)
				return FALSE;

#ifdef _UNICODE
			PathUtils::W w(buf.get());

			content = (wchar_t *)w;
#else
			content = buf.get();
#endif
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
}
